package br.com.tas.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import br.com.tas.dao.interfaces.ICoordinatesDao;
import br.com.tas.dao.util.GenericDao;
import br.com.tas.model.CoordinatesBean;

public class CoordinatesDao extends GenericDao<CoordinatesBean> implements ICoordinatesDao {

	private int idxId;
	private int idxProfile;
	private int idxAddress;
	private int idxLatitude;
	private int idxLongitude;
	
	public CoordinatesDao(Context context) {
		super(context);
	}

	@Override
	public CoordinatesBean fillAttributes(Cursor c) {
		findIdxCol(c);
		CoordinatesBean coordinatesBean = new CoordinatesBean();
		
		coordinatesBean.set_id(c.getInt(idxId));
		coordinatesBean.setProfile(c.getString(idxProfile));
		coordinatesBean.setAddress(c.getString(idxAddress));
		coordinatesBean.setLatitude(c.getString(idxLatitude));
		coordinatesBean.setLongitude(c.getString(idxLongitude));
		
		return coordinatesBean;
	}

	@Override
	public List<CoordinatesBean> fillAttributesList(Cursor c) {
		findIdxCol(c);
		
		List<CoordinatesBean> coordinatesBeans = new ArrayList<CoordinatesBean>();
		
		do{
			CoordinatesBean coordinatesBean = new CoordinatesBean();
			
			coordinatesBean.set_id(c.getInt(idxId));
			coordinatesBean.setProfile(c.getString(idxProfile));
			coordinatesBean.setAddress(c.getString(idxAddress));
			coordinatesBean.setLatitude(c.getString(idxLatitude));
			coordinatesBean.setLongitude(c.getString(idxLongitude));
			
			coordinatesBeans.add(coordinatesBean);
		}while(c.moveToNext());
		
		return coordinatesBeans;
	}

	@Override
	public ContentValues fillValues(CoordinatesBean entity) {
		ContentValues values = new ContentValues();
		
		values.put(CoordinatesBean.PROFILE, entity.getProfile());
		values.put(CoordinatesBean.ADDRESS, entity.getAddress());
		values.put(CoordinatesBean.LATITUDE, entity.getLatitude());
		values.put(CoordinatesBean.LONGITUDE, entity.getLongitude());
		
		return values;
	}
	
	private void findIdxCol(Cursor c){
		idxId = c.getColumnIndex(CoordinatesBean.ID);
		idxProfile = c.getColumnIndex(CoordinatesBean.PROFILE);
		idxAddress = c.getColumnIndex(CoordinatesBean.ADDRESS);
		idxLatitude = c.getColumnIndex(CoordinatesBean.LATITUDE);
		idxLongitude = c.getColumnIndex(CoordinatesBean.LONGITUDE);		
	}
}
